<template>
  <a-modal title="用户等级" :after-close="reset" @ok="submit">
    <a-alert message="请勿频繁更改，以免计算产生混乱！" type="warning" />
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ style: { width: '80px' } }">
      <a-form-item label="用户等级：">
        <a-select v-model:value="form.levelId" placeholder="请选择" clearable filterable multiple>
          <a-select-option v-for="(item, index) in levelList" :key="index" :value="item.id">
            {{ item.name }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item v-if="form.levelId < levelId" label="扣除经验" name="isSub">
        <a-switch v-model:checked="form.isSub" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import { userLevelUpdateApi } from "./api";
import { validReg } from '@/utils/validate';
import type { Iuser } from "./entity";
import type { ILevel } from "@/types/user";

const props = withDefaults(defineProps<{
  data: Partial<Iuser>
  levelList: ILevel[]
}>(), {
})
const levelId = computed(() => props.data.level)
const { formRef, submit, close } = useDialog({ validCallback: save })

let form = $ref({ levelId: 0, uid: 0, isSub: false })
watchEffect(() => {
  if (props.data) {
    form.uid = props.data.uid as number

  }
})

const rules = {
  usergroupName: [validReg('required', '请输入用户名')]
}

onMounted(() => {
})

const emit = defineEmits(['change'])

function save() {
  userLevelUpdateApi(form).then(() => {
    AntMessage.success('保存成功')
    close()
    emit('change')
  })
}

function reset() {
  form = { levelId: 0, uid: 0, isSub: false }
}
</script>

<style lang="less"></style>
